package com.wy.powerbuilder.controller.nxr;

import com.wy.powerbuilder.entity.nxr.*;
import com.wy.powerbuilder.mapper.nxr.ConsultingRentMapper;
import com.wy.powerbuilder.msg.MobileMessageSend;
import com.wy.powerbuilder.service.nxr.ConsultingRentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
public class ConsultingRentController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private ConsultingRentService consultingRentService;

    @Autowired
    private ConsultingRentMapper consultingRentMapper;

    @RequestMapping("/SelListAll")
    //分页查询
    public String SelList(Model model){
        List<ConsultingRent> list = consultingRentService.SelAll();
        model.addAttribute("list",list);
        return "article-list";
    }

    //根据ID查询信息
    @RequestMapping("/SelIdRent")
    public String SelId(Integer rentid, Model model){
        RentItem rentItem = consultingRentService.SelId(rentid);
        System.out.println(rentItem.getRent_content());
        model.addAttribute("rentItem",rentItem);
        return "article-info";
    }

    //查询所有状态
    @RequestMapping("/AddRent")
    public String SelectRent(Model model){
        List<TextType> textTypes = consultingRentMapper.SelTextType();
        List<ItemCategory> itemCategories = consultingRentMapper.SelItemCategory();
        List<ConsultState> consultStates = consultingRentMapper.SeleConsultState();
        model.addAttribute("textTypes",textTypes);
        model.addAttribute("itemCategories",itemCategories);
        model.addAttribute("consultStates",consultStates);
        return "article-add";
    }

    //添加

    /**
     * @return
     */
    @RequestMapping("/Insert")
    public String HH(ConsultingRent consultingRent, HttpServletRequest request, @RequestParam("text_id") Integer text_id,@RequestParam("item_id") Integer item_id) throws IOException {
        ItemCategory ic = new ItemCategory();
        ConsultState cs = new ConsultState();
        TextType tt = new TextType();
        ic.setItem_id(item_id);
        tt.setText_id(text_id);
        cs.setState_id(2);
        consultingRent.setRent_id(0);
        consultingRent.setItemCategory(ic);
        consultingRent.setTextType(tt);
        consultingRent.setConsultState(cs);
        consultingRent.setRent_date(new Date());
        consultingRent.setRent_author("林小胖");
        int row = consultingRentService.InsertRent(consultingRent);
        System.out.println(row);
        if(row >=1){
          return "redirect:/SelListAll";
        }else{
          return "500";
        }
    }

    //撤销
    @RequestMapping("/UpCX")
    public String Update1(Integer rent_id){
        int i = consultingRentService.UpdateRent1(rent_id);
        if(i>0){
            return "redirect:/SelListAll";
        }else {
            return "500";
        }
    }

    //发布
    @RequestMapping("/UpFB")
    public String Update2(Integer rent_id){
        int row = consultingRentService.UpdateRent2(rent_id);
        if(row>0){
            return "forward:/SelListAll";
        }else {
            return "500";
        }
    }

    @RequestMapping("front")
    public String Dome(){
        return "front-login";
    }

    @RequestMapping("/frontt")
    public String Dome1(){
        return "front-add";
    }

    @RequestMapping("/Gonggao")
    public String DDD(){
        return "Gonggaos/TiannengLogin";
    }

    //登录
    @RequestMapping("/GonggaoLogining")
    public String Dome2(@RequestParam("phone")String phone,@RequestParam("yzm")String yzm,Model model){
            //从session中去出来手机号跟验证码
         String sessionOtp = (String) request.getSession().getAttribute(phone);
         if(!StringUtils.equals(sessionOtp,yzm)){
            model.addAttribute("msg","电话或验证码不对，请重新输入！");
            return "Gonggaos/TiannengLogin";
         }else {
             return "redirect:/GGGGG";
         }
    }

    @RequestMapping("GGGGG")
    public String DOme3(Model model){
        List<ConsultingRent> list = consultingRentService.SelectCont1();
        List<ConsultingRent> list1 = consultingRentService.SelectRent1();
        model.addAttribute("list",list);
        model.addAttribute("list1",list1);
        return "Gonggaos/index";
    }

    @RequestMapping("GongaoSeltId")
    public String Dome4(Integer id,Model model){
        ConsultingRent rent = consultingRentService.Seleccccc(id);
        model.addAttribute("rent",rent);
        return "Gonggaos/TIannengInfo";
    }

    @RequestMapping("/Seletete")
    public String Dome5(){
        return "Gonggaos/about";
    }

    @RequestMapping("/Sproducts")
    public String Dome6(){
        return "Gonggaos/products";
    }

    @RequestMapping("/Classiccase")
    public String Dome7(){
        return "Gonggaos/productslist";
    }

    @RequestMapping("/CheckPhone")
    @ResponseBody
    public String Dome8(String phone) throws IOException {
        // String optCode = MobileMessageSend.sendMsg(phone);
        Random random = new Random();
        int i = random.nextInt(900000);
        i = i+100000;//i的取值范围时多少 100000-999999
        //UUID.randomUUID();//能生成一个不重复的字符串 分布式  量大  推特
        //讲验证码放到请求(session)中
        String optCode = String.valueOf(i);

        System.out.println(optCode);
        request.getSession().setAttribute(phone,optCode);
        System.out.println("电话===========>"+phone+"验证码=========>"+optCode);
        return "true";
    }
}
